package com.utc.teamcenter.common.app.dialog.bean;

import java.awt.Dimension;
import java.util.Arrays;
import java.util.Collection;

import javax.swing.JComponent;
import javax.swing.JScrollPane;

import com.teamcenter.rac.util.list.iList;

public class BeanIList extends BeanComponent {

	private iList list;

	private JScrollPane scrollPane;

	public BeanIList(String key, String name, String value,
			Collection<?> collection, int width, int height) {
		super(key, name);

		this.list = new iList(collection);
		this.scrollPane = new JScrollPane(this.list);
		this.list.setSelectedValue(value, true);
		this.scrollPane.setPreferredSize(new Dimension(width, height));
	}

	@Override
	public void clear() {
		this.list.clearSelection();
	}

	@Override
	public JComponent getComponent() {
		return this.scrollPane;
	}

	@Override
	public String getText() {
		Object value = this.list.getSelectedValue();

		if (value == null) {
			return null;
		}
		return this.list.getSelectedValue().toString();
	}

	@Override
	public boolean isRequired() {
		return false;
	}

	@Override
	public void setText(String text) {
	}

	@Override
	public void setRequired(boolean required) {
	}

	@Override
	public void requestFocus() {
		this.list.requestFocus();
	}

	@Override
	public void setEnabled(boolean bool) {
		this.list.setEnabled(bool);
	}

	@Override
	public void setVisible(boolean bool) {
		this.list.setVisible(bool);
	}

	public iList getList() {
		return list;
	}
	
	public String[] getSelectedValues() {
		Object[] values = this.list.getSelectedValues();

		if (values == null) {
			return null;
		}
		
		String[] vs = new String[values.length];
		for(int i=0; i<values.length; i++) {
			vs[i] = values[i].toString();
		}
		return vs;
	}
}
